package cn.gson.springboot.model.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.DrugDao;
import cn.gson.springboot.model.dao.InventoryDao;
import cn.gson.springboot.model.mapper.DrugstorageMapper;
import cn.gson.springboot.model.pojos.Category;
import cn.gson.springboot.model.pojos.Drug;
import cn.gson.springboot.model.pojos.Inventory;
import cn.gson.springboot.model.pojos.Supplier;

@Service
@Transactional(rollbackFor = Exception.class)
public class DrugstorageService {
	@Autowired
	private DrugstorageMapper drugmapper; 	//药品Mapper层
	
	@Autowired
	private DrugDao ddao;
	
	@Autowired
	private InventoryDao invdao;//库存表dao
	
	//添加药品
	public void AddDrug(Drug entity) {
		Drug drug = ddao.save(entity);
		// drug.getDrugNumber();
		Inventory iv = new Inventory();
		iv.setDrug(drug);
		iv.setInventoryQuantity(0);
		invdao.save(iv);
	}
	
	//查询最新药品id
	public JSONArray findDrugNumber() {
		JSONArray findDrugNumber = drugmapper.findDrugNumber();
		Integer drug_number=findDrugNumber.getJSONObject(0).getInteger("drug_number");
		return findDrugNumber;
	}
	
	
	//查询所有药品
	public JSONArray FindAllDrug(String drugname) {		
		return drugmapper.findalldrug(drugname);
	}
	
	//查询供应商所有药品
	public JSONArray findsuplier(Integer supplierno) {		
		return drugmapper.findsuplier(supplierno);
	}
	
	//查询药品详细批次
	public JSONArray finddrugbatch(Integer drugnumber) {		
		return drugmapper.finddrugbatch(drugnumber);
	}
	
	//查询药品类别
	public JSONArray FindByCatefory() {
		return drugmapper.FindByCatefory();		
	}
	
	//查询供应商
	public JSONArray allsupplier() {
		return drugmapper.allsupplier();		
	}
	
	//根据类别查询药品类型
	public JSONArray FindByType(Integer categoryid) {
		return drugmapper.FindByType(categoryid);		
	}
	
	//查询盘点表
	public JSONArray findchecktable() {
		return drugmapper.findchecktable();	
	}
	
	//查询盘点记录表
	public JSONArray finddrugcheck() {
		return drugmapper.finddrugcheck();	
	}
	
	//根据盘点单号查询盘点详情
	public JSONArray findcheckdetails(Integer drugchekid) {
		return drugmapper.findcheckdetails(drugchekid);	
	}
	
	//查询退药记录 
	public JSONArray findreturndrugjl() {
		return drugmapper.findreturndrugjl();	
	}
	
	//查询退药表 
	public JSONArray findreturndrug() {
		return drugmapper.findreturndrug();	
	}
	
	//根据退药单号查询退药详情
	public JSONArray findturndrugjl(Integer returngoodsid) {
		return drugmapper.findturndrugjl(returngoodsid);		
	}
	
	//查询销毁记录 
	public JSONArray finddestroyjl() {
		return drugmapper.finddestroyjl();	
	}
	
	//查询销毁i表 
	public JSONArray finddestroy() {
		return drugmapper.finddestroy();	
	}
	
	//根据销毁单号查询销毁详情
	public JSONArray finddestroydetails(Integer destroyid) {
		return drugmapper.finddestroydetails(destroyid);		
	}

}
